home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / lh211src.zip / UTIL.C < prev    next >
C/C++ Source or Header  |  1991-02-14  |  4KB  |  231 lines

  1. /***********************************************************
  2.     util.c -- utility routines
  3. ***********************************************************/
  4. #include <stdio.h>
  5. #include <io.h>
  6. #include <string.h>
  7. #include <conio.h>
  8. #include <dos.h>
  9. #include <ctype.h>
  10. #include <time.h>
  11. #include <stdlib.h>
  12. #include <stdarg.h>
  13. #include "lh.h"
  14. #include "intrface.h"
  15. #include "errmes.h"
  16.  
  17. uchar pathdelim = '\\';
  18. uchar swchar;
  19.  
  20. /***************************************
  21.     convert path delimiter
  22. ****************************************
  23.     returns *filename
  24. ***************************************/
  25. char *convdelim(char *path, char delim)
  26. {
  27.     uchar c;
  28.     uchar *p;
  29.     int kflg;
  30.  
  31.     kflg = 0;
  32.     for (p = path; (c = *p) != 0; p++) {
  33.         if (kflg) {
  34.             kflg = 0;
  35.         } else if (iskanji(c)) {
  36.             kflg = 1;
  37.         } else if (c == '\\' || c == DELIM || c == DELIM2) {
  38.             *p = delim;
  39.             path = p + 1;
  40.         }
  41.     }
  42.     return path;
  43. }
  44.  
  45. char *getfilename(char *path)
  46. {
  47.     char *q;
  48.  
  49.     if ((q = strrchr(path, DELIM)) != NULL ||
  50.         (q = strchr(path, ':')) != NULL) {
  51.         return q + 1;
  52.     } else {
  53.         return path;
  54.     }
  55. }
  56.  
  57. /*******************************
  58.   get back to parent directory
  59. *******************************/
  60. char *backpath(char *p)
  61. {
  62.     char *q;
  63.  
  64.     q = getfilename(p);
  65.     *q = '\0';
  66.     return q;
  67. }
  68.  
  69. /*******************************
  70.         ask 'Y' or 'N'
  71. *******************************/
  72. int getyn(void)
  73. {
  74.     int yn;
  75.  
  76.     do {
  77.         yn = getch();
  78.         yn = toupper(yn);
  79.         if (yn == '\x03') error(CTRLBRK, NULL);
  80.     } while (yn != 'Y' && yn != 'N');
  81.     eprintf("%c\n", yn);
  82.     return yn;
  83. }
  84.  
  85. void getswchar(void)
  86. {
  87.     union REGS regs;
  88.  
  89.     regs.x.ax = 0x3700;
  90.     intdos(®s, ®s);
  91.     swchar = regs.h.dl;
  92.     if (swchar != '/') {
  93.         pathdelim = '/';
  94.     }
  95. }
  96.  
  97. time_t dos2unix(struct ftime *ft)
  98. {
  99.     struct tm tm;
  100.  
  101.     tm.tm_sec   = ft -> ft_tsec * 2;
  102.     tm.tm_min   = ft -> ft_min;
  103.     tm.tm_hour  = ft -> ft_hour;
  104.     tm.tm_mday  = ft -> ft_day;
  105.     tm.tm_mon   = ft -> ft_month - 1;
  106.     tm.tm_year  = ft -> ft_year + 80;
  107.     tm.tm_isdst = timezone;
  108.     return mktime(&tm);
  109. }
  110.  
  111. struct ftime *unix2dos(time_t utc)
  112. {
  113.     static struct ftime ft;
  114.     struct tm *tm;
  115.  
  116.     tm = localtime(&utc);
  117.     ft.ft_tsec     = tm -> tm_sec / 2;
  118.     ft.ft_min      = tm -> tm_min ;
  119.     ft.ft_hour     = tm -> tm_hour;
  120.     ft.ft_day      = tm -> tm_mday;
  121.     ft.ft_month    = tm -> tm_mon  + 1;
  122.     ft.ft_year     = tm -> tm_year - 80;
  123.     return &ft;
  124. }
  125.  
  126. /*******************************
  127.         ratio * 1000
  128. *******************************/
  129. int ratio(ulong a, ulong b, int ord)
  130. {
  131.     int i;
  132.  
  133.     for (i = 0; i < ord && a < 0x19999999; i++) {
  134.         a *= 10;                /* while not overflow */
  135.     }                            /* upto 10^ord times */
  136.     for (; i < ord; i++) {        /* the case of overflow */
  137.         b /= 10;
  138.     }
  139.     if (b == 0) return 0;        /* if diviser == 0 */
  140.     a += b / 2;                    /* for round up */
  141.     return (a / b);                /* return (a * 1000 / b) */
  142. }
  143.  
  144. #define BUFFERSIZE 0x6000
  145.  
  146. void copyfile(FILE *f1, FILE *f2, long size, int crc_flg)
  147. {
  148.     int xsize;
  149.     int dicsiz;
  150.     void dispmark(char c);
  151.     void fwrite_crc(uchar *p, int n, FILE *f);
  152.  
  153.     crc = 0;
  154.     if (f2) fflush(f2);
  155.     dicsiz = 1 << interface.dicbit;
  156.     while (size > 0) {
  157.         xsize = (size > BUFFERSIZE) ? BUFFERSIZE : size;
  158.         if (fread(text, 1, xsize, f1) != xsize) {
  159.             fileerror(RDERR, f1);
  160.         }
  161.         fwrite_crc(text, xsize, f2);
  162.         size -= xsize;
  163.         if (crc_flg) {
  164.             while (xsize > 0) {
  165.                 dispmark('c');
  166.                 xsize -= dicsiz;
  167.             }
  168.         }
  169.     }
  170. }
  171.  
  172. void far *e_farmalloc(ulong size)
  173. {
  174.     void far *p;
  175.  
  176.     if ((p = farmalloc(size)) == NULL) error(MEMOVRERR, NULL);
  177.     return p;
  178. }
  179.  
  180. void *e_malloc(uint size)
  181. {
  182.     void *p;
  183.  
  184.     if ((p = malloc(size)) == NULL) error(MEMOVRERR, NULL);
  185.     return p;
  186. }
  187.  
  188. void *e_realloc(void *buf, int size)
  189. {
  190.     void *p;
  191.  
  192.     if ((p = realloc(buf, size)) == NULL) error(MEMOVRERR, NULL);
  193.     return p;
  194. }
  195.  
  196. FILE *myeopen(char *path, char *mode, char *errmes)
  197. {
  198.     FILE *f;
  199.     int fd;
  200.     extern int _creat(char *path, int attr);
  201.  
  202.     f = NULL;
  203.     if (*mode != 'w') {
  204.         f = fopen(path, mode);
  205.     } else {
  206.         fd = _creat(path, 0x20);
  207.         if (fd >= 0) f = fdopen(fd, mode);
  208.     }
  209.     if (f == NULL && errmes != NULL) error(errmes, path);
  210.     return f;
  211. }
  212.  
  213. FILE *mywopen(char *path, char *errmes)
  214. {
  215.     return myeopen(path, "wb", errmes);
  216. }
  217.  
  218. FILE *myropen(char *path)
  219. {
  220.     return myeopen(path, "rb", NOFILEERR);
  221. }
  222.  
  223. void eprintf(char *fmt, ...)
  224. {
  225.     va_list p;
  226.  
  227.     va_start(p, fmt);
  228.     vfprintf(stderr, fmt, p);
  229.     va_end(p);
  230. }
  231.